package com.example.zjx8969.myapplication.database;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import android.text.TextUtils;

import com.example.zjx8969.myapplication.compare.LookInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Dao
public abstract class LookDao {
    @Query("select * from lookInfo")
    abstract List<LookInfo> getAll();

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    public abstract void insert(List<LookInfo> data);

    @Query("select * from lookInfo where `key`=:key")
    public abstract LookInfo find(String key);

    public Map<String, LookInfo> getAllMap() {
        List<LookInfo> allLook = getAll();
        if (allLook == null) return null;
        Map<String, LookInfo> result = new HashMap<>(allLook.size());
        for (LookInfo lookInfo : allLook) {
            if (TextUtils.isEmpty(lookInfo.key)) continue;
            result.put(lookInfo.key, lookInfo);
        }
        return result;
    }

    public void insertAllMap(Map<String, LookInfo> dataSet) {
        if (dataSet == null || dataSet.isEmpty()) return;

        List<LookInfo> result = new ArrayList<>(dataSet.size());
        for (Map.Entry<String, LookInfo> item : dataSet.entrySet()) {
            if (TextUtils.isEmpty(item.getKey())) continue;
            item.getValue().key = item.getKey();
            result.add(item.getValue());
        }
        insert(result);
    }
}
